#ifndef TRIAL_UART_PROTOCOL_H
#define TRIAL_UART_PROTOCOL_H
#include "stm32f1xx_hal.h"
#include "platform.h"
//#pragma pack (1)
typedef struct __protocol_usart_send_frame_format_0
{
    uint8_t ReserveBit : 4;     // 预留
    uint8_t ReserveDataFlag : 3;  // 保留位。默认为0.内部测试R[2:0]用作数据标识：
    uint8_t FrameHeaderFlag : 1;       //帧头标识位。为常数1

}ProtocolMultiTurnFrame0;

typedef struct __protocol_usart_send_frame_format_1
{
    uint8_t MultiTurn : 3;   // 多圈[23:21]
    uint8_t ReserveBit1 : 4;
    uint8_t Flag_0 : 1;
}ProtocolMultiTurnFrame1;
typedef struct __protocol_usart_send_frame_format_2
{
    uint8_t MultiTurn : 7;   // 多圈[20:14]
    uint8_t Flag_0 : 1;
}ProtocolMultiTurnFrame2;

typedef struct __protocol_usart_send_frame_format_3
{
    uint8_t MultiTurn : 7;   // 多圈[13:7]
    uint8_t Flag_0 : 1;
}ProtocolMultiTurnFrame3;

typedef struct __protocol_usart_send_frame_format_4
{
    uint8_t MultiTurn : 7;   // 多圈[6:0]
    uint8_t Flag_0 : 1;
}ProtocolMultiTurnFrame4;

typedef struct __protocol_usart_send_frame_format_5
{
    uint8_t ABS_Pos : 3;   // 位置[23:21]
    uint8_t ReserveBit2 : 4;//预留[3:0]
    uint8_t Flag_0 : 1;
}ProtocolMultiTurnFrame5;

typedef struct __protocol_usart_send_frame_format_6
{
    uint8_t ABS_Pos : 7;   // 位置[20:14]
    uint8_t Flag_0 : 1;
}ProtocolMultiTurnFrame6;

typedef struct __protocol_usart_send_frame_format_7
{
    uint8_t ABS_Pos : 7;   // 位置[13:7]
    uint8_t Flag_0 : 1;
}ProtocolMultiTurnFrame7;

typedef struct __protocol_usart_send_frame_format_8
{
    uint8_t ABS_Pos : 7;   // 位置[6:0]
    uint8_t Flag_0 : 1;
}ProtocolMultiTurnFrame8;

typedef struct __protocol_usart_send_frame_format_9
{
    uint8_t ReserveBit3 : 7; // 预留[6:0]
    uint8_t Flag_0 : 1;
}ProtocolMultiTurnFrame9;

typedef struct __protocol_usart_send_frame_format_10
{
    uint8_t Valid : 1;        // 位置有效标识。0表示数据有效，1表示数据无效
    uint8_t Frame_CRC6 : 6;   //  CRC[5:0] 采输出不作校验，使用固定值6’b111111作为数据结束。
    uint8_t Flag_0 : 1;
}ProtocolMultiTurnFrame10;

/** 多圈UART 发送协议 */
typedef struct __ProtocolMultiTurnFrame
{
    ProtocolMultiTurnFrame0  Frame_0;
    ProtocolMultiTurnFrame1  Frame_1;
    ProtocolMultiTurnFrame2  Frame_2;
    ProtocolMultiTurnFrame3  Frame_3;
    ProtocolMultiTurnFrame4  Frame_4;
    ProtocolMultiTurnFrame5  Frame_5;
    ProtocolMultiTurnFrame6  Frame_6;
    ProtocolMultiTurnFrame7  Frame_7;
    ProtocolMultiTurnFrame8  Frame_8;
    ProtocolMultiTurnFrame9  Frame_9;
    ProtocolMultiTurnFrame10 Frame_10;
}PMTF;

PMTF uart_sendDataPackage();



// 解码后的多圈协议
typedef struct __ProtocolMutliTurnDecode
{
    uint8_t ValidError:1;
    uint8_t ReserveDataFlag:2;    // 数据标识 2bit
    uint8_t ReserveBit:4;         // 预留1 4bit
    uint8_t ReserveBit1:4;        // 预留2 4bit
    uint8_t ReserveBit2:4;        // 预留3 4bit
    uint8_t ReserveBit3:7;        // 预留4 7bit
    uint8_t CrcError:1;            // crc错误
    uint32_t MutliTurnCnt;        // 多圈 24bit
    uint32_t AbsPoseCnt;          // 绝对位置 24bit

}ProtocolMutliTurnDecode;

void DMA_UART_Init();
uint8_t crc6(uint8_t *data, uint16_t length);
ProtocolMutliTurnDecode Rx_DecodeToMultiTurn(const uint8_t* rxByteArray,uint16_t size);
int8_t DecodeToMultiTurn(uint8_t* pData,uint16_t size,ProtocolMutliTurnDecode* dstValue);

// CRC 计算值与读取值校验
static int crc6Check( uint8_t* pData,uint8_t length)
{
    uint8_t crc6calc = crc6(pData,length-1);
    uint8_t crc6read = (pData[length-1] & 0x7E)>>1;
    return crc6calc==crc6read;
}
#endif //TRIAL_UART_PROTOCOL_H

